//
//  NYMyExamineDetailHeaderCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/22.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYMyExamineDetailHeaderCell.h"
#import "AAChartView.h"
@interface NYMyExamineDetailHeaderCell ()
@property(nonatomic, strong) TTTAttributedLabel *titleLabel;
@property(nonatomic, strong) TTTAttributedLabel *desLabel;
@property(nonatomic, strong) TTTAttributedLabel *subTitleLabel;
@property(nonatomic, strong) TTTAttributedLabel *subDesLabel;
@property(nonatomic, strong) UIView *pieChartContentView;
@property (nonatomic, strong) AAChartView *pieChart;
@property(nonatomic, strong) AAChartModel *aaChartModel;
@end
@implementation NYMyExamineDetailHeaderCell
+ (CGFloat)heightForObject:(NICellObject *)object identifier:(NSString *)identifier atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
        return [tableView fd_heightForCellWithIdentifier:identifier cacheByIndexPath:indexPath configuration:^(NYMyExamineDetailHeaderCell * cell) {
                cell.detailModel = object.userInfo;
        }];
}
- (BOOL)shouldUpdateCellWithObject:(NICellObject *)object atIndexPath:(NSIndexPath *)indexPath
{
        self.detailModel = object.userInfo;
        return YES;
}
- (void)setDetailModel:(NYMyExamineDetailModel *)detailModel
{
        _detailModel = detailModel;
        [self updateContent];
}
- (void)updateContent
{
        if (!self.detailModel)
        {
                return;
        }
        self.titleLabel.text = self.detailModel.page_info.examine_title;
        if (!NULLString(self.detailModel.page_info.courses_title))
        {
                self.titleLabel.text = [NSString stringWithFormat:@"%@ %@",NOT_NULL_STRING_DEFAULT_EMPTY(self.detailModel.page_info.courses_title),NOT_NULL_STRING_DEFAULT_EMPTY(self.detailModel.page_info.examine_title)];
        }
        
        NSString *desString = [NSString stringWithFormat:@"本试卷总%d题，共计100分,第一次得分为%d分，本次得分%d分",self.detailModel.page_info.num.intValue,self.detailModel.page_info.achievement.intValue,self.detailModel.page_info.examine_score.intValue];
        [self.desLabel setText:desString afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
                NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"\\d+" options:(0) error:nil];
                [[regular matchesInString:mutableAttributedString.string options:(0) range:NSMakeRange(0, mutableAttributedString.string.length)] enumerateObjectsUsingBlock:^(NSTextCheckingResult * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (idx > 1) {
                                [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)(UIColorHex(0xFF6F40).CGColor) range:obj.range];
                        }
                }];
                return mutableAttributedString;
        }];
        self.subTitleLabel.text = @"所有同学答题情况";
        NSString *subDesString = [NSString stringWithFormat:@"总答题人数%d人,回答情况如下：",self.detailModel.page_info.answer_num.intValue];
        
        [self.subDesLabel setText:subDesString afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
                NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"\\d+" options:(0) error:nil];
                [[regular matchesInString:mutableAttributedString.string options:(0) range:NSMakeRange(0, mutableAttributedString.string.length)] enumerateObjectsUsingBlock:^(NSTextCheckingResult * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)(UIColorHex(0xFF6F40).CGColor) range:obj.range];
                }];
                return mutableAttributedString;
        }];
        if (self.detailModel.score_statistics!=nil && self.detailModel.score_statistics.count > 0) {
                [self createPieChartView];
        } else {
                [self.pieChart mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(self.pieChartContentView.mas_top);
                        make.height.mas_equalTo(__WGHeight(0));
                        make.left.right.bottom.equalTo(self.pieChartContentView);
                }];
        }
}
- (void)createPieChartView
{
        //        [self.pieChartContentView removeAllSubviews];
        NSMutableArray *items = [NSMutableArray arrayWithCapacity:self.detailModel.score_statistics.count];
        NSMutableArray *colors = [NSMutableArray arrayWithCapacity:self.detailModel.score_statistics.count];
        [self.detailModel.score_statistics enumerateObjectsUsingBlock:^(NYMyExamineScoreModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [items addObject:@[[NSString stringWithFormat:@"%@  %.2f%%",obj.group_name,obj.score.floatValue],obj.score.numberValue]];
                [colors addObject:obj.color];
        }];
        AAChartModel *aaChartModel= AAObject(AAChartModel)
        .chartTypeSet(AAChartTypePie)
        .colorsThemeSet(colors)
        .titleSet(@"")
        .subtitleSet(@"")
        .dataLabelEnabledSet(true)//是否直接显示扇形图数据
        .yAxisTitleSet(@"总百分比")
        .subtitleAlignSet(AAChartSubtitleAlignTypeCenter)
        .seriesSet(@[AAObject(AASeriesElement)
                     .nameSet(@"总百分比")
                     .innerSizeSet(@"30%")
                     .dataSet(items)]);
        self.aaChartModel = aaChartModel;
        [self.pieChart aa_drawChartWithChartModel:aaChartModel];
}

- (void)setupView
{
        [super setupView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.desLabel];
        [self.contentView addSubview:self.subTitleLabel];
        [self.contentView addSubview:self.subDesLabel];
        self.pieChartContentView = [[UIView alloc] init];
        self.pieChartContentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.pieChartContentView];
        
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).offset(__WGHeight(24));
                make.left.equalTo(self.contentView).offset(__WGWidth(22));
                make.right.equalTo(self.contentView).offset(-__WGWidth(22));
                make.centerX.equalTo(self.contentView.mas_centerX);
        }];
        
        [self.desLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(__WGHeight(18));
                make.left.right.equalTo(self.titleLabel);
                make.height.mas_equalTo(__WGHeight(30));
        }];
        
        [self.subTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.desLabel.mas_bottom).offset(__WGHeight(25));
                make.left.right.equalTo(self.titleLabel);
        }];
        
        [self.subDesLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.subTitleLabel.mas_bottom).offset(__WGHeight(38));
                make.left.equalTo(self.contentView).offset(__WGWidth(15));
                make.right.equalTo(self.contentView).offset(-__WGWidth(15));
        }];
        
        [self.pieChartContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.subDesLabel.mas_bottom).offset(__WGHeight(23));
                make.left.right.bottom.equalTo(self.contentView);
        }];
        
        self.pieChart = [[AAChartView alloc] init];
        self.pieChart.isClearBackgroundColor = YES;
        [self.pieChartContentView addSubview:self.pieChart];
        
        [self.pieChart mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.pieChartContentView.mas_top);
                make.height.mas_equalTo(__WGHeight(400));
                make.left.right.bottom.equalTo(self.pieChartContentView);
        }];
}

/// MARK: - Lazy
- (TTTAttributedLabel *)titleLabel
{
        if (_titleLabel == nil) {
                _titleLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
                _titleLabel.font = WGSystem18Font;
                _titleLabel.textColor = UIColorHex(0x333333);
                _titleLabel.textAlignment = NSTextAlignmentCenter;
                [_titleLabel setContentCompressionResistancePriority:(UILayoutPriorityDefaultLow) forAxis:(UILayoutConstraintAxisHorizontal)];
        }
        return _titleLabel;
}
- (TTTAttributedLabel *)desLabel
{
        if (_desLabel == nil) {
                _desLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
                _desLabel.font = WGSystem11Font;
                _desLabel.textColor = UIColorHex(0x999999);
                _desLabel.textAlignment = NSTextAlignmentCenter;
                _desLabel.layer.cornerRadius = 15;
                _desLabel.clipsToBounds = YES;
                _desLabel.textInsets = UIEdgeInsetsMake(0, __WGWidth(8), 0, __WGWidth(8));
                _desLabel.backgroundColor = UIColorForBackground;
                [_desLabel setContentCompressionResistancePriority:(UILayoutPriorityDefaultLow) forAxis:(UILayoutConstraintAxisHorizontal)];
        }
        return _desLabel;
}
- (TTTAttributedLabel *)subTitleLabel
{
        if (_subTitleLabel == nil) {
                _subTitleLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
                _subTitleLabel.font = WGSystem14Font;
                _subTitleLabel.textColor = UIColorHex(0x333333);
                _subTitleLabel.textAlignment = NSTextAlignmentCenter;
                [_subTitleLabel setContentCompressionResistancePriority:(UILayoutPriorityDefaultLow) forAxis:(UILayoutConstraintAxisHorizontal)];
        }
        return _subTitleLabel;
}
- (TTTAttributedLabel *)subDesLabel
{
        if (_subDesLabel == nil) {
                _subDesLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
                _subDesLabel.font = WGSystem13Font;
                _subDesLabel.textColor = UIColorHex(0x333333);
                [_subDesLabel setContentCompressionResistancePriority:(UILayoutPriorityDefaultLow) forAxis:(UILayoutConstraintAxisHorizontal)];
        }
        return _subDesLabel;
}


@end
